﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.SharePoint;

namespace CSSoft.VNTower.Domain
{
    public class Constants
    {
        public const string DATE_FORMAT = "dd/MM/yyyy";

        public const string LIST_USER_INFOMATION = "UserInfomation";
        public const string LIST_FUNCTIONS = "Functions";
        public const string LIST_PROJECTS = "Projects";
        public const string LIST_DAILY_REPORT = "Daily Report";
        public const string LIST_DAILY_REPORT_FILES = "Daily Report Files";
        public const string LIST_EMPLOYEE_TASKS = "Employee Tasks";
        public const string LIST_WEEKLY_REPORT = "Weekly Report";
        public const string LIST_WEEKLY_REPORT_TASKS = "Weekly Report Tasks";
    }
    public static class DailyFileCounts
    {
        private static Dictionary<DateTime, int> Counter = new Dictionary<DateTime, int>();

        public static int FileInDate(DateTime date)
        {
            int result = 1;
            try
            {
                if (Counter.ContainsKey(date))
                {
                    result = Counter[date];
                    Counter[date] = ++result;
                }
                else
                {
                    try
                    {
                        SPList list = CS2Web.GetList(Constants.LIST_DAILY_REPORT_FILES);
                        SPQuery query = new SPQuery();
                        query.Query = String.Format(@"<Where>
	                         <Eq>
		                        <FieldRef Name='Date' />
		                        <Value IncludeTimeValue='FALSE' Type='DateTime'>{0}</Value>
	                         </Eq>
                           </Where>
                           <OrderBy>
                              <FieldRef Name='ID' Ascending='False' />
                           </OrderBy>", date.ToString(CS2SPQueryBuilder.QUERY_DATE_FORMAT));
                        SPListItemCollection items = list.GetItems(query);
                        if (CS2SPQueryBuilder.HasItems(items))
                        {
                            string fileCount = CS2Convert.ToString(items[0]["FileLeafRef"]);
                            fileCount = fileCount.Substring(9, 3);
                            result = CS2Convert.ToInt(fileCount) + 1;
                        }
                        Counter.Add(date, result);
                    }
                    catch { }
                }
            } catch { }
            return result;
        }
    }
}
